package com.platform.api.bzproject.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.platform.common.core.Result;
import com.platform.common.util.EmojiFilterUtil;
import com.platform.security.constants.AppValue;
import com.platform.security.enums.App;
import com.platform.security.model.AppConnect;
import com.platform.security.service.AppConnectService;
import com.platform.security.util.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * -- 源码联系微信：2549789059
 * @author guoyunfei on 2021/11/28 11:57
 * @className AppConnectControllr
 * @description TODO
 * @Version 1.0
 */

@RestController
@RequestMapping("{appid}/u/appConnect")
public class AppConnectControllr {

    @Autowired
    private AppConnectService appConnectService;


    @PostMapping("setUserInfo")
    public Result setUserInfo(@RequestBody AppConnect appConnect, HttpServletRequest request){

        try {
            String userId = SecurityUtils.getUser().getUserId();
            Integer systemType = StringUtils.isNotBlank(request.getHeader("systemType")) ? Integer.valueOf(request.getHeader("systemType")) : 0;
            App appValue = AppValue.getAppValue(systemType);

            AppConnect connecta = appConnectService.getOne(new QueryWrapper<AppConnect>()
                    .eq("user_id", userId).last(" limit 1"));

            if(null != connecta){
                AppConnect connect = appConnectService.getByBizUserId(connecta.getOpenid(), appValue);
                if(null != connect){

                    appConnect.setId(connect.getId());
                    appConnect.setImageUrl(appConnect.getAvatarUrl());
                    String s = EmojiFilterUtil.filterEmoji(appConnect.getNickName());
                    appConnect.setNickName(s);
                    appConnectService.updateById(appConnect);
                }
            }
        }catch (Exception e){

        }

        return Result.success();
    }


}
